name: MinGW
on:
  push:
    paths-ignore:
      - 'doc/**'
      - '**/man'
      - '**.md'
      - '**.rdoc'
      - '**/.document'
  pull_request:
    paths-ignore:
      - 'doc/**'
      - '**/man'
      - '**.md'
      - '**.rdoc'
      - '**/.document'
  merge_group:
    paths-ignore:
      - 'doc/**'
      - '**/man'
      - '**.md'
      - '**.rdoc'
      - '**/.document'

concurrency:
  group: ${{ github.workflow }} / ${{ startsWith(github.event_name, 'pull') && github.ref_name || github.sha }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull') }}

permissions:
  contents: read

# Notes:
# Actions console encoding causes issues, see test-all & test-spec steps
#
jobs:
  make:
    runs-on: windows-2022
    name: ${{ github.workflow }} (${{ matrix.msystem }})
    env:
      MSYSTEM: ${{ matrix.msystem }}
      MSYS2_ARCH: x86_64
      CHOST: "x86_64-w64-mingw32"
      CFLAGS:   "-march=x86-64 -mtune=generic -O3 -pipe"
      CXXFLAGS: "-march=x86-64 -mtune=generic -O3 -pipe"
      CPPFLAGS: "-D_FORTIFY_SOURCE=2 -D__USE_MINGW_ANSI_STDIO=1 -DFD_SETSIZE=2048"
      LDFLAGS:  "-pipe"
      GITPULLOPTIONS: --no-tags origin ${{github.ref}}
    strategy:
      matrix:
        include:
          # To mitigate flakiness of MinGW CI, we test only one runtime that newer MSYS2 uses.
          - msystem: "UCRT64"
            base_ruby: head
            test_task: "check"
            test-all-opts: "--name=!/TestObjSpace#test_reachable_objects_during_iteration/"
      fail-fast: false
    if: ${{ !contains(github.event.head_commit.message, '[DOC]') && !contains(github.event.pull_request.labels.*.name, 'Documentation') }}
    steps:
      - run: mkdir build
        working-directory:
      - name: git config
        run: |
          git config --global core.autocrlf false
          git config --global core.eol lf
          git config --global advice.detachedHead 0
          git config --global init.defaultBranch garbage
      - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
        with:
          path: src
      - uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
        with:
          path: src/.downloaded-cache
          key: downloaded-cache
      - name: Set up Ruby & MSYS2
        uses: ruby/setup-ruby@250fcd6a742febb1123a77a841497ccaa8b9e939 # v1.152.0
        with:
          ruby-version: ${{ matrix.base_ruby }}
      - name: set env
        run: |
          echo "GNUMAKEFLAGS=-j$((2 * NUMBER_OF_PROCESSORS))" >> $GITHUB_ENV

      - name: where check
        run: |
          # show where
          mv /c/Windows/System32/libcrypto-1_1-x64.dll /c/Windows/System32/libcrypto-1_1-x64.dll_
          mv /c/Windows/System32/libssl-1_1-x64.dll    /c/Windows/System32/libssl-1_1-x64.dll_
          result=true
          for e in gcc.exe ragel.exe make.exe libcrypto-1_1-x64.dll libssl-1_1-x64.dll; do
            echo '##['group']'$'\033[93m'$e$'\033[m'
            where $e || result=false
            echo '##['endgroup']'
          done
          $result

      - name: version check
        run: |
          # show version
          result=true
          for e in gcc ragel make "openssl version"; do
            case "$e" in *" "*) ;; *) e="$e --version";; esac
            echo '##['group']'$'\033[93m'$e$'\033[m'
            $e || result=false
            echo '##['endgroup']'
          done
          $result

      - name: autogen
        run: |
          ./autogen.sh
        working-directory: src

      - name: configure
        run: >
          ../src/configure --disable-install-doc --prefix=/.
          --build=$CHOST --host=$CHOST --target=$CHOST

      - name: update
        run: |
          make incs

      - name: download gems
        run: |
          make update-gems

      - name: make all
        timeout-minutes: 30
        run: |
          make

      - name: make install
        run: |
          make DESTDIR=../install install-nodoc

      - name: test
        timeout-minutes: 30
        run: |
          make test
        env:
          GNUMAKEFLAGS: ""
          RUBY_TESTOPTS: "-v --tty=no"
        if: ${{matrix.test_task == 'check' || matrix.test_task == 'test'}}

      - name: test-all
        timeout-minutes: 45
        run: |
          # Actions uses UTF8, causes test failures, similar to normal OS setup
          chcp.com 437
          make ${{ StartsWith(matrix.test_task, 'test/') && matrix.test_task || 'test-all' }}
        env:
          RUBY_TESTOPTS: >-
            --retry --job-status=normal --show-skip --timeout-scale=1.5
            ${{ matrix.test-all-opts }}
          BUNDLER_VERSION:
        if: ${{matrix.test_task == 'check' || matrix.test_task == 'test-all' || StartsWith(matrix.test_task, 'test/')}}

      - name: test-spec
        timeout-minutes: 10
        run: |
          make ${{ StartsWith(matrix.test_task, 'spec/') && matrix.test_task || 'test-spec' }}
        if: ${{matrix.test_task == 'check' || matrix.test_task == 'test-spec' || StartsWith(matrix.test_task, 'spec/')}}

      - uses: ruby/action-slack@0bd85c72233cdbb6a0fe01d37aaeff1d21b5fce1 # v3.2.1
        with:
          payload: |
            {
              "ci": "GitHub Actions",
              "env": "${{ github.workflow }} ${{ matrix.msystem }} / ${{ matrix.test_task }}",
              "url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
              "commit": "${{ github.sha }}",
              "branch": "${{ github.ref_name }}"
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SIMPLER_ALERTS_URL }} # ruby-lang slack: ruby/simpler-alerts-bot
        if: ${{ failure() && github.event_name == 'push' }}

defaults:
  run:
    working-directory: build
    shell: sh
